<?php

namespace Frieza\Modules\CommunityMcn\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Doraemon\model\CommunityMcn\Brief;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\service\CommunityMcn\Brief as sBrief;
use Doraemon\service\CommunityMcn\AdminRole;

class BriefListController extends Controller
{

    public function list(Request $request, Content $content)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 20);
        $statusType = $request->input('status', 0);
        $limit = [($page - 1) * $pageSize, $pageSize];
        $order = ['invite_time' => 'DESC'];
        if($statusType){
            $where['status'] = $statusType;
            $where['close_time[>=]'] = time();
        }
        $isBusiness = AdminRole::isBusiness();
        if($isBusiness){
            $where['admin_uid'] = Admin::user()->id;
        }
        $list = sBrief::getListByWhere($where, $limit, $order);
        $data = self::setData($list['list']);
        $gridModel = new pGridDataModel($data, $list['total'], ['perPage' => $pageSize]);
        $listGrid = Admin::grid($gridModel, function (Grid $grid){
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableFilter();
            $grid->column('id')->hide();
            $grid->column('close_time')->hide();
            $grid->column('status')->hide();
        //    $grid->column('project_name', '项目名称');
            $grid->column('project_name', '项目名称')->display(function ($name){
                return $name;
            });
            $grid->column('invite_time', '邀约时间');
            $grid->column('popularize_brand', '合作品牌');
            $grid->column('collaborative_area', '合作领域');
            $grid->column('status_name', '状态');
            $grid->column('price_style', '结算方式');
            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableEdit();
                $actions->disableView();
                $actions->disableDelete();
                $id = $actions->row->id;
                $status = $actions->row->status;
                switch ($status) {
                    case Brief::STATUS_CLOSE:
                    case Brief::STATUS_CHECK:
                        $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/briefInfo/' . $id . '/read" >项目详情</a><br>');
                        break;
                    case Brief::STATUS_DISPATCH:
                        $isBusiness = AdminRole::isBusiness();
                        if(!$isBusiness){
                            $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/inviteMember/'.$id.'/index/create" >邀约派单</a><br>');
                        }
                        $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/briefInfo/' . $id . '/read" >项目详情</a><br>');

                        break;
                    case Brief::STATUS_CHANGE:
                        $url = '/admin/communityMcn/suggestion/index?b_id=' . $id;
                        $actions->prepend('<a class="fa fa-indent reload-data" href="' . $url . '" >修改意见</a><br>');
                        $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/briefInfo/' . $id . '/read" >项目详情</a><br>');
                        break;
                    case Brief::STATUS_DISPATCH_SUCCESS:
                        $url = '/admin/communityMcn/inviteMember/'.$id.'/index';
                        $actions->prepend('<a class="fa fa-indent reload-data" href="' . $url . '" >邀约人员名单</a><br>');
                        $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/briefInfo/' . $id . '/read" >项目详情</a><br>');
                        break;
                }
            });
        });
        $tab = new Tab();
        switch ($statusType){
            case Brief::STATUS_CHECK:
                $tab->addLink('全部', '/admin/communityMcn/briefInfo/index');
                $tab->add('待审核', $listGrid->render(), true);
                $tab->addLink('待修改', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHANGE);
                $tab->addLink('待派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH);
                $tab->addLink('已派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH_SUCCESS);
                break;
            case Brief::STATUS_CHANGE:
                $tab->addLink('全部', '/admin/communityMcn/briefInfo/index');
                $tab->addLink('待审核', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHECK);
                $tab->add('待修改', $listGrid->render(), true);
                $tab->addLink('待派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH);
                $tab->addLink('已派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH_SUCCESS);
                break;
            case Brief::STATUS_DISPATCH:
                $tab->addLink('全部', '/admin/communityMcn/briefInfo/index');
                $tab->addLink('待审核', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHECK);
                $tab->addLink('待修改', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHANGE);
                $tab->add('待派单', $listGrid->render(), true);
                $tab->addLink('已派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH_SUCCESS);
                break;
            case Brief::STATUS_DISPATCH_SUCCESS:
                $tab->addLink('全部', '/admin/communityMcn/briefInfo/index');
                $tab->addLink('待审核', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHECK);
                $tab->addLink('待修改', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHANGE);
                $tab->addLink('待派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH);
                $tab->add('已派单', $listGrid->render(), true);
                break;
            default:
                $tab->add('全部', $listGrid->render(), true);
                $tab->addLink('待审核', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHECK);
                $tab->addLink('待修改', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_CHANGE);
                $tab->addLink('待派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH);
                $tab->addLink('已派单', '/admin/communityMcn/briefInfo/index?status=' . Brief::STATUS_DISPATCH_SUCCESS);
                break;

        }
        $content->row($tab);
        return $content
            ->header('MCN项目管理')
            ->breadcrumb(['text' => 'MCN项目管理']);
    }

    public static function setData($data = array())
    {
        $brandList = sBrief::getBrandList();
        $list = array();
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['project_name'] = $value['project_name'];
            $list[$key]['invite_time'] = $value['invite_time'] ? date('Y-m-d H:i:s', $value['invite_time']) : '';
            $list[$key]['popularize_brand'] = $value['brand_inner'] == 1 ? $brandList[$value['popularize_brand_id']] : $value['popularize_brand_name'];
            $list[$key]['price_style'] = Brief::PRICE_STYLE_LIST[$value['price_style']];
            $list[$key]['status'] = $value['status'];
            if($value['close_time'] < time()){
                $list[$key]['status'] = Brief::STATUS_CLOSE;
            }

            if($value['close_time'] > time() && ($value['close_time'] - Brief::RED_SECONDS) <= time() ){
                $name = $value['project_name'];
                $list[$key]['project_name'] = "<span style='color: red;'>$name</span>";
            }
            $list[$key]['status_name'] = Brief::STATUS_LIST[$list[$key]['status']];
            $list[$key]['collaborative_area'] = self::areaToString($value['collaborative_area']);
            $list[$key]['countDownTime'] = Brief::CLOSE_SECONDS + time();
        }
        return $list;
    }

    private static function areaToString($areas)
    {
        if (!$areas) return '';
        $list = explode(',', $areas);
        $str = '';
        foreach ($list as $value) {
            $str .= Brief::AREA_LIST[$value] . ' ';
        }
        return rtrim($str);
    }

}

